<?php

namespace App\Models\Member;

use App\Models\BaseModel;
use App\Models\Member\GoodType;
use App\Models\Member\GoodPrice;
use App\Common\AliossHandler;
use Auth;

class Good extends BaseModel
{
    protected $fillable = [
        'id', 'store_id', 'good_type_id', 'sale_type',
        'title', 'cover', 'describe', 'detail', 
        'sku', 'base_price', 'weight', 'status', 
        'jf', 'jf_send', 'share_money', 'cost', 'sort', 'is_delivery_fee', 'list_num',
        'created_at', 'updated_at'
    ];
    // 传递 店铺ID 和 指定商品iD数组 获取商品价格信息
    public function storeGoods($storeId, $goodsArray)
    {
        return $this->whereIn('id',$goodsArray)->where('store_id', $storeId)
        ->where('status',1)
        ->get()->toArray();
    }

    // 遍历以商品ID为键的购物车数据 得出 合法商品ID数组 / 零售价 / 应得积分
    public function foreachCart($info, $goodsData) 
    {
        $goodsKeyVal = key_val($goodsData,'id');
        $data = [];
        $data['retailPrice'] = 0;// 零售价
        $data['pointsAll'] = 0;// 可得积分
        $data['goodsNewArray'] = [];// 合法商品数组
        foreach ($info as $key => $val) {
            $id = $val['id'];
            if (!isset($goodsKeyVal[$id])) {
                continue;
            }
            
            if ($val['pitch'] == true) {
                $num = $val['num'];
                $basePrice = $goodsKeyVal[$id]['base_price'];
                $jfSend = $goodsKeyVal[$id]['jf_send'];
                $data['retailPrice'] += $num*$basePrice;
                $data['pointsAll'] += $num*$jfSend;
            }
            $data['goodsNewArray'][] = $id;
        }
        return $data;
    }

    public function getType()
    {
        $storeId = Auth::user()->store_id;
        $goodType = new GoodType;
        $arr = ['id'=>0,'title'=>'全部商品'];
        $types = $goodType->getType($storeId);
        array_unshift($types,$arr);
        return $types;
    }

    public function getMall($typeId, $word)
    {
        $storeId = Auth::user()->store_id;
        $sql = $this->join('good_prices as p','p.good_id','=','goods.id')
                    ->where('goods.store_id', $storeId) 
                    ->where('status', 1)
                    ->where('sale_type', 1)
                    ->where('title', 'like', '%'.$word.'%')
                    ->orderBy('sort', 'desc')
                    ->select(['goods.id', 'title', 'cover', 'base_price','price','jf']);
        //要求不管点击什么分类都显示全部商品
        // if($typeId != 0){
        //     $sql = $this->where('good_type_id', $typeId);
        // }

        $data = $sql->get()->toArray();
        $data = $this->handleGoodList($data);
        return $data;
    }

    public function getHot($type)
    {
        $storeId = Auth::user()->store_id;
        $data = $sql = $this->join('good_prices as p','p.good_id','=','goods.id')
                    ->where('goods.store_id', $storeId) 
                    ->where('status', 1)
                    ->where('sale_type', $type)
                    ->orderBy('sort', 'desc')
                    ->select(['goods.id', 'title', 'cover', 'base_price','price','describe','jf'])
                    ->get()
                    ->toArray();
        $data = $this->handleGoodList($data);
        return $data;
    }

    private function handleGoodList($data)
    {
        foreach ($data as $key => $item) {
            $data[$key]['cover'] = AliossHandler::read('good', $item['cover']);
            //取消多等级价格 只保留会员价格
            $data[$key]['price'] = $item['price'] / 100;
            $data[$key]['base_price'] = $item['base_price'] / 100;
        }

        return $data;
    }

    public function getDetail($id)
    {
        $user = Auth::user()->toArray();
        $storeId = $user['store_id'];
        $grade = $user['grade'];
        $data = $this->find($id)->toArray();
        $data['cover'] = AliossHandler::read('good', $data['cover']);
        $data['base_price'] = $data['base_price'] / 100;
        $data['price'] = GoodPrice::where('store_id', $storeId)->where('level', 0)->where('good_id', $data['id'])->first()->price / 100;

        return $data;
    }
}
